import com.henry.service.SysTplCodeService;
import com.henry.utils.GeneratorCode;
import com.henry.web.vo.SysTplCodeRequestBean;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipOutputStream;

/**
 * Created by 峥桂 on 2017/5/27.
 */
public class TesTplCode extends BaseTest {

    @Autowired
    private GeneratorCode generatorCode;

    @Autowired
    private SysTplCodeService sysTplCodeService;

    @Test
    public void testTplCode1() {
        sysTplCodeService.findAllSysTplCode(new SysTplCodeRequestBean()).forEach(tplcode-> System.out.println(tplcode.getTplName()));
    }
    @Test
    public void testTplCode2(){

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        generatorCode.zipFileDefault("sys_tpl_code",zip,null);
        IOUtils.closeQuietly(zip);
        try {
            IOUtils.write(outputStream.toByteArray(), new FileOutputStream("d://generatorMyCode//sys_tpl_code.zip"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testYuLan(){
        String tplContent=getControllerTpl();
         tplContent=entityTpl();
         tplContent=mapperTpl();
         tplContent=mapperxmlTpl();
         tplContent=serviceImplTpl();
        System.out.println();
        System.out.println(generatorCode.yuLan("demo_city",tplContent));
    }
    private String defaultTpl(){
        return "";
    }
    private String getControllerTpl(){
        return "package ${package}.contorller;\n" +
                "\n" +
                "import org.springframework.stereotype.Controller;\n" +
                "import org.springframework.web.bind.annotation.RequestMapping;\n" +
                "#if(${superControllerClassPackage})\n" +
                "import ${superControllerClassPackage};\n" +
                "#end\n" +
                "\n" +
                "/**\n" +
                " * <p>\n" +
                " * $!{comments} 前端控制器\n" +
                " * </p>\n" +
                " *\n" +
                " * @author ${author}\n" +
                " * @since ${datetime}\n" +
                " */\n" +
                "@Controller\n" +
                "@RequestMapping(\"${classname}\")\n" +
                "#if(${superControllerClass})\n" +
                "public class ${className} extends ${superControllerClass} {\n" +
                "#else\n" +
                "public class ${className}Controller {\n" +
                "#end\n" +
                "\n" +
                "\n" +
                "}";
    }
    private String mapperTpl(){
        return "package ${package}.dao.mapper;\n" +
                "\n" +
                "import${package}.dao.entity.${className};\n" +
                "import com.baomidou.mybatisplus.mapper.BaseMapper;\n" +
                "\n" +
                "/**\n" +
                " * <p>\n" +
                " * $!{comments} Mapper 接口\n" +
                " * </p>\n" +
                " *\n" +
                " * @author ${author}\n" +
                " * @email ${email}\n" +
                " * @date ${datetime}\n" +
                " */\n" +
                " */\n" +
                "public interface ${className}Mapper extends BaseMapper<${className}> {\n" +
                "\n" +
                "}";
    }
    private String entityTpl(){
        return "package ${package}.dao.entity;\n" +
                "\n" +
                "import java.io.Serializable;\n" +
                "import java.util.Date;\n" +
                "import com.baomidou.mybatisplus.annotations.TableName;\n" +
                "\n" +
                "#if(${hasBigDecimal})\n" +
                "import java.math.BigDecimal;\n" +
                "#end\n" +
                "\n" +
                "\n" +
                "/**\n" +
                " * ${comments}\n" +
                " *\n" +
                " * @author ${author}\n" +
                " * @email ${email}\n" +
                " * @date ${datetime}\n" +
                " */\n" +
                "@TableName(\"${tableName}\")\n" +
                "public class ${className} implements Serializable {\n" +
                "    private static final long serialVersionUID = 1L;\n" +
                "\n" +
                "#foreach ($column in $columns)\n" +
                "    //$column.comments\n" +
                "#if($!pk.columnName==$column.columnName)\n" +
                "    @TableId(value=\"$column.columnName\", type= IdType.AUTO)\n" +
                "#else\n" +
                "    @TableField(\"$column.columnName\")\n" +
                "#end\n" +
                "    private $column.attrType $column.attrname;\n" +
                "    \n" +
                "#end\n" +
                "\n" +
                "#foreach ($column in $columns)\n" +
                "    /**\n" +
                "     * 设置：${column.comments}\n" +
                "     */\n" +
                "    public void set${column.attrName}($column.attrType $column.attrname) {\n" +
                "        this.$column.attrname = $column.attrname;\n" +
                "    }\n" +
                "    /**\n" +
                "     * 获取：${column.comments}\n" +
                "     */\n" +
                "    public $column.attrType get${column.attrName}() {\n" +
                "        return $column.attrname;\n" +
                "     }\n" +
                "#end\n" +
                "}";
    }
    private String mapperxmlTpl(){
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n" +
                "<mapper namespace=\"${package}.dao.mapper.${className}Mapper\">\n" +
                "    <resultMap id=\"BaseResultMap\" type=\"${package}.dao.entity.${className}\">\n" +
                "#foreach($column in $columns)\n" +
                "        <result column=\"$column.columnName\" property=\"${column.attrname}\" />\n" +
                "#end\n" +
                "    </resultMap>\n" +
                "\n" +
                "    <sql id=\"Base_Column_List\">#foreach($column in $columns)`$column.columnName`#if($velocityCount != $columns.size()),#end#end</sql>\n" +
                "</mapper>\n";
    }
    private String serviceImplTpl(){
        return "package ${package}.service.impl;\n" +
                "\n" +
                "import ${package}.dao.entity.${className};\n" +
                "import ${package}.dao.mapper.${className}Mapper;\n" +
                "import ${package}.service.${className}Service;\n" +
                "import com.baomidou.mybatisplus.service.impl.ServiceImpl;\n" +
                "import org.springframework.stereotype.Service;\n" +
                "\n" +
                "/**\n" +
                " * <p>\n" +
                " *   服务实现类\n" +
                " * </p>\n" +
                " *\n" +
                " * @author Yanghu\n" +
                " * @since 2017-05-31\n" +
                " */\n" +
                "@Service\n" +
                "public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements ${className}Service {\n" +
                "\n" +
                "}\n";
    }

}
